package com.wz.project.pda.controller;

import com.wz.framework.web.domain.AjaxResult;
import com.wz.project.api.barcode.domain.Barcode;
import com.wz.project.api.barcode.domain.BarcodeInfo;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.barcode.service.BarcodeService;
import com.wz.project.api.sap.controller.SapController;
import com.wz.project.api.tmsMatnr.domain.TmMatnr;
import com.wz.project.api.tmsMatnr.service.ITmMatnrService;
import com.wz.project.barcodeMsg.domain.BarcodeMsg;
import com.wz.project.barcodeMsg.service.IBarcodeMsgService;
import com.wz.project.bas.product.domain.Product;
import com.wz.project.bas.product.service.IProductService;
import com.wz.project.inStock.stockin.domain.InStockin;
import com.wz.project.inStock.stockindetail.domain.InStockinDetail;
import com.wz.project.mydo.domain.MydoJobDetail;
import com.wz.project.mydo.mydojob.service.IMydoJobService;
import com.wz.project.mydo.mydojobdetail.service.IJobDetailService;
import com.wz.project.outStock.stockout.domain.OutStockout;
import com.wz.project.outStock.stockout.service.IOutStockoutService;
import com.wz.project.pda.service.PdaService;
import com.wz.project.sap.DN.domain.ZflWmsGetDn;
import com.wz.project.sap.DN.service.IZflWmsGetDnService;
import com.wz.project.sap.DN_POST_DN.domain.ZflWmsDnPostDn;
import com.wz.project.sap.DN_POST_DN.service.IZflWmsDnPostDnService;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.stock.service.IBasStockService;
import com.wz.project.stock.stockdetail.domain.BasStockDetail;
import com.wz.project.stock.stockdetail.service.IBasStockDetailService;
import com.wz.project.surebarcode.sureBarcode.domain.Surebarcode;
import com.wz.project.surebarcode.sureBarcode.service.ISurebarcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pdaPk1")
public class PdaPController {

    private String prefix = "rf";

    @Autowired
    private IOutStockoutService outStockoutService;
    @Autowired
    PdaService pdaService;
    @Autowired
    SapController sapController;
    @Autowired
    ITmMatnrService matnrService;
    @Autowired
    IMydoJobService mydoJobService;
    @Autowired
    IJobDetailService mydoJobDetailService;
    @Autowired
    IZflWmsGetDnService ZflWmsGetDnService;
    @Autowired
    IProductService productService;
    @Autowired
    IZflWmsDnPostDnService zflWmsDnPostDnService;
    @Autowired
    IBarcodeMsgService barcodeMsgService;
    @Autowired
    BarcodeService barcodeService;
    @Autowired
    IBasStockDetailService basStockDetailService;
    @Autowired
    IBasStockService basStockService;
    @Autowired
    EntityManager entityManager;

    @Autowired
    ISurebarcodeService surebarcodeService;

    @GetMapping("/productsh")
    public String productsh() {
        return prefix + "/productsh";
    }

    @GetMapping("/productInP")
    public String productInP() {
        return prefix + "/productInP";
    }

    @GetMapping("/barcodeget")
    public String barcodeget() {
        return prefix + "/barcodeget";
    }

    @GetMapping("/recover")
    public String recover() {
        return prefix + "/recover";
    }

    @GetMapping("/productzj")
    public String DN_POST_DN() {
        return prefix + "/productzj";
    }

    /**
     * 确认收货
     *
     * @param fbarcode
     * @param trayCode
     * @param cargoSpaceId
     * @return
     */
    @RequestMapping("/barcodesave")
    @ResponseBody
    public AjaxResult barcodesave(String fbarcode, String trayCode, String cargoSpaceId) {
        AjaxResult ajaxResult = new AjaxResult();
        BarcodeMsg barcodeMsg1 = barcodeMsgService.selectBarcodeMsgById(fbarcode);
        if (barcodeMsg1 == null) {
            BarcodeMsg barcodeMsg = new BarcodeMsg();
            barcodeMsg.setTrayCode(trayCode);
            barcodeMsg.setBarcode(fbarcode);
            barcodeMsg.setCargoSpaceId(cargoSpaceId);
            barcodeMsg.setCreateTime(new Date());
            barcodeMsgService.insertBarcodeMsg(barcodeMsg);
            ajaxResult.put("res", "数据采集成功");
        } else {
            ajaxResult.put("res", "条码数据重复");
        }
        return ajaxResult;

    }

    /**
     * 收货确认数据获取
     *
     * @param fbarcode
     * @param stockInId
     * @return
     */
    @RequestMapping("/tms/listSH")
    @ResponseBody
    public Map selectTmsMsgSH(String fbarcode, String stockInId) {
        Map map = new HashMap<>();
        //查询是否同步交货单号
        ZflWmsGetDn zflWmsGetDn = ZflWmsGetDnService.selectZflWmsGetDnById1(String.format("%010d", Long.parseLong(stockInId)));
        if (zflWmsGetDn == null) {
            sapController.getDNData(String.format("%010d", Long.parseLong(stockInId)), "", "");
        }
        //查询条码是否已入库
        BasStockDetail basStockDetail = basStockDetailService.selectBasStockDetailById(fbarcode);
        if (basStockDetail != null) {
            map.put("isnull", "该条码已入库，无需重复扫描");
            return map;
        }
        //查询计算该单据所有子件总数
        InStockinDetail inStockinDetail1 = pdaService.selectInstock(String.format("%010d", Long.parseLong(stockInId)));
        if (inStockinDetail1 != null) {
            map.put("qty", inStockinDetail1.getQuantity());
        }
        //查询条码是否已扫描未入库
        TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
        if (tmMatnr1 != null) {
            //按照单据，物料子件分组统计各物料扫描数量
            List<TmMatnr> tmMatnr = matnrService.selectTmMatnrByInstock(String.format("%010d", Long.parseLong(stockInId)));
            if (tmMatnr.size() > 0) {
                map.put("matnrList", tmMatnr);
            }
            //查询该单据已扫描总数
            List<TmMatnr> tmMatnrl = matnrService.selectTmMatnrByStickInIde(String.format("%010d", Long.parseLong(stockInId)));
            map.put("qtyout", tmMatnrl.size());
            map.put("isnull", "该条码已扫描，无需重复扫描");
            return map;
        }
        //查询物料子母件信息
        Barcode barcode = null;
        try {
            Barcode barcode1 = pdaService.selectBarcodeBd(fbarcode);
            if (barcode1 == null) {
                barcode = barcodeService.selectBarcodeByIdHs(fbarcode);
                if (barcode != null) {
                    pdaService.saveBarcode(barcode);
                }
            } else {
                barcode = barcode1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isnull", "条码平台异常！！！");
            return map;
        }
        String stockinGuid = "";
        if (barcode != null) {

            //生产订单号（成品）在v_tm_matnr_minfo只有一条信息
            if (barcode.getAufnr() != null && barcode.getAufnr() != "") {
                stockinGuid = barcode.getAufnr();
                map.put("isnull", "该条码非配套物资无需收货");
                return map;
            } else {
                //采购订单号（配套）在v_tm_matnr_minfo有多条信息
                stockinGuid = String.format("%010d", Long.parseLong(stockInId));
            }
            //用物料和单据号查询子单据信息，如果有此物料的单据
            String barcodes = String.format("%018d", Long.parseLong(barcode.getMatnr().split("-")[0]));
            List<InStockinDetail> inStockinDetails = pdaService.selectInstockDetail(stockinGuid, barcodes);
            if (inStockinDetails != null) {
                InStockinDetail inStockinDetail = inStockinDetails.get(0);
                List<TmMatnr> matnrList = pdaService.selectTmMatnrList(stockinGuid, barcode.getMatnr());
                if (matnrList.size() >= inStockinDetail.getQuantity().intValue()) {
                    map.put("isnull", "该物料子件已超过交货单数量");
                    List<TmMatnr> tmMatnr = matnrService.selectTmMatnrByInstock(String.format("%010d", Long.parseLong(stockInId)));
                    if (tmMatnr.size() > 0) {
                        map.put("matnrList", tmMatnr);
                    }
                    List<TmMatnr> tmMatnrl = matnrService.selectTmMatnrByStickInIde(String.format("%010d", Long.parseLong(stockInId)));

                    map.put("qtyout", tmMatnrl.size());
                    return map;
                }
                List<MatnrInfo> matnrInfoList = null;
                List<MatnrInfo> matnrInfoLists = pdaService.selectMatnrInfobd(barcode.getMatnr().split("-")[0]);
                if (matnrInfoLists.size() == 0) {
                    //从v_tm_matnr_minfo表中用母件码查询所有齐套件信息
                    matnrInfoList = pdaService.selectMatnrInfo(barcode.getMatnr().split("-")[0]);
                    if (matnrInfoList.size() > 0) {
                        for (MatnrInfo m : matnrInfoList) {
                            pdaService.insetMatnrInfo(m);
                        }
                    } else {
                        map.put("isnull", "此物料信息子母件条码平台未维护");
                        return map;
                    }
                }
                MatnrInfo matnrInfo = pdaService.selectMatnrBdById(barcode.getMatnr());
                TmMatnr tmMatnr = new TmMatnr();
                tmMatnr.setMatnr(matnrInfo.getMatnr());
                tmMatnr.setMaktx(matnrInfo.getMaktx());
                tmMatnr.setMatnrMain(barcodes);
                tmMatnr.setYkutxt(matnrInfo.getYkutxt());
                tmMatnr.setEwbez(matnrInfo.getEwbez());
                tmMatnr.setFunit(matnrInfo.getFunit());
                tmMatnr.setFpackunit(matnrInfo.getFpackunit());
                tmMatnr.setFflag(matnrInfo.getFflag());
                tmMatnr.setFnum(matnrInfo.getFnum());
                tmMatnr.setFbarcode(barcode.getFbarcode());
                tmMatnr.setStatus(-1);
                tmMatnr.setLotNo(new SimpleDateFormat("yyyyMMdd").format(new Date()));
                tmMatnr.setInStockId(inStockinDetail.getStockinGuid());
                tmMatnr.setInStockDId(inStockinDetail.getStockinDetailId());
                matnrService.insertTmMatnr(tmMatnr);

                map.put("isnull", "该物料子件扫描成功");
                List<TmMatnr> tmMatnrl = matnrService.selectTmMatnrByStickInIde(String.format("%010d", Long.parseLong(stockInId)));
                map.put("matnrList", tmMatnrl);
                map.put("qtyout", tmMatnrl.size());
            } else {
                map.put("isnull", "无对应的收货单");
            }
        } else {
            map.put("isnull", "此条码条码平台未进行维护");
        }
        return map;
    }

    /**
     * 确认收货
     *
     * @param stockInId
     * @return
     */
    @RequestMapping("/SureSH")
    @ResponseBody
    public AjaxResult SureSH(String stockInId) {
        AjaxResult ajaxResult = new AjaxResult();
        stockInId = String.format("%010d", Long.parseLong(stockInId));
        InStockinDetail inStockinDetail1 = pdaService.selectInstock(stockInId);
        int fnum = 0;
        if (inStockinDetail1 != null) {
            fnum = inStockinDetail1.getQuantity().intValue();
        }
        List<TmMatnr> tmMatnrs = matnrService.selectTmMatnrByStickInIde(stockInId);
        int saomiaosize = matnrService.selectTmMatnrGroupProduct(stockInId);
        int instockincount = pdaService.selectCount(stockInId);
        if (tmMatnrs.size() < fnum || saomiaosize < instockincount) {
            ajaxResult.put("status", "该条码未齐套，无法收货");
            return ajaxResult;
        }

        for (TmMatnr tmMatnr : tmMatnrs) {
            if (tmMatnr != null && tmMatnr.getStatus() == -1 && "1".equals(tmMatnr.getFflag())) {
                String productLotNo = "";//是否批次管理
                String productXlh = "";//序列号
                Product product = productService.selectProductByProductId(String.format("%018d", Long.parseLong(tmMatnr.getMatnr().split("-")[0])));
                if (product != null) {
                    productLotNo = product.getSpecialFlag();
                    productXlh = product.getProductFlag();
                }
                ZflWmsGetDn zflWmsGetDn = new ZflWmsGetDn();
                zflWmsGetDn.setMATNR(String.format("%018d", Long.parseLong(tmMatnr.getMatnr().split("-")[0])));
                zflWmsGetDn.setVBELN(tmMatnr.getInStockId());
                List<ZflWmsGetDn> zflWmsGetDns = ZflWmsGetDnService.selectZflWmsGetDnList(zflWmsGetDn);
                for (ZflWmsGetDn zflWmsGetDna : zflWmsGetDns) {
                    String lotNo = "";
                    ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                    if ("X".equals(productLotNo)) {
                        zflWmsDnPostDn.setCHARG(tmMatnr.getLotNo());
//                        zflWmsDnPostDn.setCHARG(new SimpleDateFormat("yyyyMMdd").format(new Date()));
                    }
                    if ("X".equals(productXlh)) {
                        zflWmsDnPostDn.setSERNR(tmMatnr.getFbarcode().substring(4, tmMatnr.getFbarcode().length()));
                        zflWmsDnPostDn.setLFIMG(tmMatnr.getFnum().toString());
                    } else {
                        zflWmsDnPostDn.setLFIMG(zflWmsGetDna.getLFIMG());
                    }

                    zflWmsDnPostDn.setZEILE(zflWmsGetDna.getPOSNR());
                    zflWmsDnPostDn.setVBELN(zflWmsGetDna.getVBELN());
                    zflWmsDnPostDn.setPOSNR(zflWmsGetDna.getPOSNR());
                    zflWmsDnPostDn.setMATNR(zflWmsGetDna.getMATNR());
                    zflWmsDnPostDn.setPIKMG("0");
                    zflWmsDnPostDn.setWERKS(zflWmsGetDna.getWERKS());
                    zflWmsDnPostDn.setLGORT(zflWmsGetDna.getLGORT());
                    zflWmsDnPostDn.setVRKME(zflWmsGetDna.getVRKME());

                    int size = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn).size();
                    zflWmsDnPostDn.setStatus("-1");
                    if (size == 0) {
                        zflWmsDnPostDnService.insertZflWmsDnPostDn(zflWmsDnPostDn);
                    }
                }

                ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                zflWmsDnPostDn.setVBELN(stockInId);
                zflWmsDnPostDn.setStatus("-1");
                int size = zflWmsDnPostDnService.selectZflWmsDnPostDnCount(zflWmsDnPostDn);
                int i = pdaService.selectCountAll(String.format("%010d", Long.parseLong(stockInId)));
                if (size == i) {
                    zflWmsDnPostDn.setStatus("0");
                    zflWmsDnPostDnService.updateZflWmsDnPostDnStatus(zflWmsDnPostDn);
                    pdaService.updatemMatnrs(stockInId);
                }
                ajaxResult.put("status", "收货成功");
            } else if (tmMatnr != null && tmMatnr.getStatus() == 0) {
                ajaxResult.put("status", "已收货");
            }
        }
        return ajaxResult;

    }

    /**
     * 质检
     *
     * @param prueflos
     * @param type
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    public AjaxResult check(String prueflos, String type) {
        AjaxResult ajaxResult = new AjaxResult();

        ZflWmsDnPostDn zflWmsDnPostDn = zflWmsDnPostDnService.selectZflWmsDnPostDisticnctByprueflos(prueflos);
        if (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getTYPE())) {
            if ("OK".equals(type)) {
                zflWmsDnPostDnService.updateZflWmsDnPostDnZjOk(prueflos);
            } else {
                zflWmsDnPostDnService.updateZflWmsDnPostDnZjNo(prueflos);
            }
            ajaxResult.put("status", "操作成功");
        } else {
            ajaxResult.put("status", "该质检已完成无需重复操作");
        }

        return ajaxResult;

    }

    @RequestMapping("/autoAllocation")
    @ResponseBody
    public Map<String, Object> autoAllocation(String stockin_guid) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        stockin_guid = String.format("%010d", Integer.parseInt(stockin_guid));
        ZflWmsGetDn zflWmsGetDn = ZflWmsGetDnService.selectZflWmsGetDnById1(stockin_guid);
        if (zflWmsGetDn != null) {
            sapController.getDNData(stockin_guid, "", "");
        }
        OutStockout outStockout = outStockoutService.selectOutStockoutById(stockin_guid);
        String stockOutId[] = new String[1];
        if (outStockout != null) {
            stockOutId[0] = outStockout.getStockoutId();
            return outStockoutService.autoAllocation(stockOutId);
        } else {
            resultMap.put("errorInfo", "没有对应交货单");
            return resultMap;
        }

    }


    @RequestMapping("/ckSure")
    @ResponseBody
    public AjaxResult ckSure(String trayCode, String fbarcode) {
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_ck_sure");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_trayCode", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_fbarcode", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", Integer.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("v_resultMsg", String.class, ParameterMode.OUT);

        parameter.setParameter("v_trayCode", trayCode);
        parameter.setParameter("v_fbarcode", fbarcode);
        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        Integer result = (Integer) storedProcedureQuery.getOutputParameterValue("v_result");
        String resultString = (String) storedProcedureQuery.getOutputParameterValue("v_resultMsg");
        return AjaxResult.success(resultString + "" + result);

    }

    @RequestMapping("/SureCkIn")
    @ResponseBody
    public HashMap<String, Object> SureCkIn(String trayCode) {
        HashMap<String, Object> map = new HashMap<>();
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("wcs_in_job");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_tray_code", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", Integer.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("v_result_msg", String.class, ParameterMode.OUT);
        parameter.setParameter("v_tray_code", trayCode);
        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        Integer result = (Integer) storedProcedureQuery.getOutputParameterValue("v_result");

        if (result == 10) {
            map.put("msg", 2);
            return map;
        }
        map.put("msg", (String) storedProcedureQuery.getOutputParameterValue("v_result_msg"));
        return map;

    }


    /**
     * 确认出库
     *
     * @param fbarcode
     * @param stockin_guid
     * @return
     */
    @RequestMapping("/sureOut")
    @ResponseBody
    public AjaxResult sureOut(String fbarcode, String stockin_guid) {
        AjaxResult ajaxResult = new AjaxResult();
//        ZflWmsGetDn zflWmsGetDn = ZflWmsGetDnService.selectZflWmsGetDnById(stockin_guid);
        stockin_guid = String.format("%010d", Long.parseLong(stockin_guid));
        Surebarcode surebarcode = new Surebarcode();
        surebarcode.setFbarcode(fbarcode);
        surebarcode.setStockoutGuid(stockin_guid);
        List<Surebarcode> surebarcodes = surebarcodeService.selectSurebarcodeList(surebarcode);

        if (surebarcodes.size() > 0) {

            Surebarcode surebarcode1 = surebarcodes.get(0);
            BasStock basStock = basStockService.selectBasStockByTray(surebarcode1.getTrayCode());
            MatnrInfo matnrInfo = null;
            matnrInfo = pdaService.selectMatnrBdById(surebarcode1.getProductDetailId());
            if (matnrInfo == null) {
                List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfo(surebarcode1.getProductDetailId().split("-")[0]);
                if (matnrInfoList.size() > 0) {
                    for (MatnrInfo m : matnrInfoList) {
                        pdaService.insetMatnrInfo(m);
                    }
                }
            }
            matnrInfo = pdaService.selectMatnrBdById(surebarcode1.getProductDetailId());
            if (matnrInfo != null) {
                MatnrInfo matnrInfo1 = pdaService.selectMatnrBdById(surebarcode1.getProductDetailId().split("-")[0]);
//                List<TmMatnr> tmMatnrs = pdaService.selectTmMatnrByfbarcode(fbarcode.substring(0, 18), fbarcode.substring(13, 22));
                List<TmMatnr> tmMatnrs = pdaService.selectTmMatnrByfbarcodea(fbarcode);
                if (tmMatnrs.size() < matnrInfo.getFnum().intValue() + 1) {
                    TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
                    if (tmMatnr1 == null) {
                        int count = pdaService.selectTmMatnrByFCount(surebarcode1.getProductDetailId(), surebarcode1.getStockoutGuid(), surebarcode1.getProductId());
                        if (count == 0) {

                            TmMatnr tmMatnr = new TmMatnr();
                            tmMatnr.setMatnr(matnrInfo.getMatnr());
                            tmMatnr.setMaktx(matnrInfo.getMaktx());
                            tmMatnr.setMatnrMain(surebarcode1.getProductId());
                            tmMatnr.setYkutxt(matnrInfo.getYkutxt());
                            tmMatnr.setEwbez(matnrInfo.getEwbez());
                            tmMatnr.setFunit(matnrInfo.getFunit());
                            tmMatnr.setFpackunit(matnrInfo.getFpackunit());
                            tmMatnr.setFflag(matnrInfo.getFflag());
                            tmMatnr.setFnum(matnrInfo.getFnum());
                            tmMatnr.setBarcode(surebarcode1.getBarcode());
                            tmMatnr.setFbarcode(fbarcode);
//                    tmMatnr.setFid(barcodeInfo1.getFid());
                            tmMatnr.setStatus(-1);
                            tmMatnr.setLotNo(surebarcode1.getLotNo());
                            tmMatnr.setInStockId(surebarcode1.getStockoutGuid());
                            tmMatnr.setInStockDId(surebarcode1.getVGPOS());
                            matnrService.insertTmMatnr(tmMatnr);
//                            List<TmMatnr> tmMatnrss = pdaService.selectTmMatnrByfbarcode(fbarcode.substring(0, 18), fbarcode.substring(13, 22));
                            List<TmMatnr> tmMatnrss = pdaService.selectTmMatnrByfbarcodea(fbarcode);
                            for (TmMatnr tmMatnra : tmMatnrss) {

                                if (tmMatnra != null && tmMatnra.getStatus() == -1 && "1".equals(tmMatnra.getFflag())) {
                                    String productLotNo = "";//是否批次管理
                                    String productXlh = "";//序列号
                                    Product product = productService.selectProductByProductId(String.format("%018d", Long.parseLong(tmMatnra.getMatnr().split("-")[0])));
                                    if (product != null) {
                                        productLotNo = product.getSpecialFlag();
                                        productXlh = product.getProductFlag();
                                    }
                                    ZflWmsGetDn zflWmsGetDn = new ZflWmsGetDn();
                                    zflWmsGetDn.setMATNR(String.format("%018d", Long.parseLong(tmMatnra.getMatnr().split("-")[0])));
                                    zflWmsGetDn.setVBELN(tmMatnra.getInStockId());
                                    List<ZflWmsGetDn> zflWmsGetDns = ZflWmsGetDnService.selectZflWmsGetDnList(zflWmsGetDn);
                                    for (ZflWmsGetDn zflWmsGetDna : zflWmsGetDns) {
                                        String lotNo = "";
                                        ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();

                                        zflWmsDnPostDn.setZEILE(zflWmsGetDna.getPOSNR());
                                        zflWmsDnPostDn.setVBELN(zflWmsGetDna.getVBELN());
                                        zflWmsDnPostDn.setPOSNR(zflWmsGetDna.getPOSNR());
                                        zflWmsDnPostDn.setMATNR(zflWmsGetDna.getMATNR());
                                        zflWmsDnPostDn.setWERKS(zflWmsGetDna.getWERKS());
                                        zflWmsDnPostDn.setLGORT(zflWmsGetDna.getLGORT());
                                        zflWmsDnPostDn.setVRKME(zflWmsGetDna.getVRKME());

                                        if ("X".equals(productLotNo)) {
                                            zflWmsDnPostDn.setCHARG(tmMatnr.getLotNo());
                                            int size = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn).size();

                                            zflWmsDnPostDn.setLFIMG(matnrInfo.getFnum().toString());
                                            zflWmsDnPostDn.setPIKMG(matnrInfo.getFnum().toString());
                                            zflWmsDnPostDn.setStatus("-1");
                                            if (size == 0) {
                                                zflWmsDnPostDnService.insertZflWmsDnPostDn(zflWmsDnPostDn);
                                            } else {
                                                ZflWmsDnPostDn zflWmsDnPostDna1 = new ZflWmsDnPostDn();
                                                zflWmsDnPostDna1.setZEILE(zflWmsGetDna.getPOSNR());
                                                zflWmsDnPostDna1.setVBELN(zflWmsGetDna.getVBELN());
                                                ZflWmsDnPostDn zflWmsDnPostDn1 = zflWmsDnPostDnService.selectZflWmsDnPostDn(zflWmsDnPostDna1);

                                                zflWmsDnPostDna1.setLFIMG((String.valueOf(new BigDecimal(zflWmsDnPostDn1.getLFIMG()).intValue() + 1)));
                                                zflWmsDnPostDna1.setPIKMG((String.valueOf(new BigDecimal(zflWmsDnPostDn1.getPIKMG()).intValue() + 1)));
                                                zflWmsDnPostDnService.updateZflWmsDnPostDn(zflWmsDnPostDna1);
                                            }
                                        }
                                        if ("X".equals(productXlh)) {
                                            zflWmsDnPostDn.setSERNR(tmMatnr.getFbarcode().substring(4, fbarcode.length()));
                                            int size = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn).size();
                                            zflWmsDnPostDn.setStatus("-1");

                                            zflWmsDnPostDn.setLFIMG(matnrInfo.getFnum().toString());
                                            zflWmsDnPostDn.setPIKMG(matnrInfo.getFnum().toString());
                                            if (size == 0) {
                                                zflWmsDnPostDnService.insertZflWmsDnPostDn(zflWmsDnPostDn);
                                            }
                                        }


                                    }
                                }
                                int i = pdaService.selectCountOut(stockin_guid);
                                ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                                zflWmsDnPostDn.setVBELN(stockin_guid);
                                zflWmsDnPostDn.setStatus("-1");
                                int size = zflWmsDnPostDnService.selectZflWmsDnPostDnCount(zflWmsDnPostDn);
                                if (size == i) {
                                    zflWmsDnPostDn.setStatus("0");
                                    zflWmsDnPostDnService.updateZflWmsDnPostDnStatus(zflWmsDnPostDn);
                                }

                            }
                            BasStockDetail basStockDetail = basStockDetailService.selectBasStockDetailById(fbarcode);
                            if (basStockDetail != null) {
                                basStockDetailService.insertBasStockDetailfinish(basStockDetail);
                                basStockDetailService.deleteBasStockDetailById(fbarcode);
                            }
                            ajaxResult.put("status", "出货成功");
                            if (tmMatnrss.size() == matnrInfo.getFnum().intValue()) {
                                TmMatnr tmMatnr11 = new TmMatnr();
                                tmMatnr11.setStatus(0);
                                tmMatnr11.setBarcode(surebarcode.getBarcode());
                                tmMatnr11.setInStockId(surebarcode.getStockoutGuid());
                                matnrService.updateTmMatnr(tmMatnr1);
                            }
                        } else {
                            ajaxResult.put("status", "该单据的物料子件数量已达分配上限");
                        }
                    } else {
                        ajaxResult.put("status", "该条码已扫描，请勿重复扫描");
                    }
                }
            }

            if ("02".equals(basStock.getWarehouseId())) {
                pdaService.finishPK();
            }
        } else {
            ajaxResult.put("status", "或者条码已出库,或扫描条码不在对应托盘");
        }

        return ajaxResult;

    }


    @RequestMapping("/tms/list")
    @ResponseBody
    public Map selectTmsMsg(String fbarcode) {
        Map map = new HashMap<>();
        //从v_tm_barcode_info存储过程中用条码查询物料及单据信息
        Barcode barcode = pdaService.selectBarcode(fbarcode);
//        Barcode barcode  =pdaService.selectBarcodeByIdHs(fbarcode);
        String stockinGuid = "";
        if (barcode != null) {
            //生产订单号（成品）在v_tm_matnr_minfo只有一条信息
            if (barcode.getAufnr() != null && barcode.getAufnr() != "") {
                stockinGuid = barcode.getAufnr();
            }
            //采购订单号（配套）在v_tm_matnr_minfo有多条信息
            if (barcode.getPurchaseno() != null && barcode.getPurchaseno() != "") {
                stockinGuid = barcode.getPurchaseno();
                map.put("isnull", "该条码为成品物资");
                return map;
            }
            //用物料和单据号查询子单据信息，如果有此物料的单据
            String barcodes = String.format("%018d", Long.parseLong(barcode.getMatnr().split("-")[0]));
            List<InStockinDetail> inStockinDetails = pdaService.selectInstockDetail(stockinGuid, barcodes);

            if (inStockinDetails != null) {
                InStockinDetail inStockinDetail = inStockinDetails.get(0);
//                String[] split = barcode.getMatnr().split("-");

                //从v_tm_matnr_minfo表中用母件码查询所有齐套件信息
                List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfo(barcode.getMatnr().split("-")[0]);
                map.put("nowMatnr", barcode.getMatnr());
                map.put("matnrList", matnrInfoList);//成品只能查出一条母件信息
                map.put("quantity", barcode.getFnum());

                //查询tm_matnr表里是否已有此条
                //从t_barcode_info表中查询fid值
                BarcodeInfo barcodeInfo = pdaService.selectBarcodeInfo(fbarcode);
                if (barcodeInfo != null) {
                    TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
                    if (tmMatnr1 != null) {
                        map.put("status", tmMatnr1.getStatus());
                        return map;
                    }
                    int size = 0;
                    String barcodeMain = "";
                    String barcodeBehind = "";
                    //成品
                    if (barcode.getAufnr() != null && barcode.getAufnr() != "") {
                        barcodeMain = fbarcode.substring(0, 13);
                        List<Barcode> barcodeList = pdaService.selectBarcodeBycode(barcodeMain);
                        size = barcodeList.size();
                    } else {
                        barcodeMain = fbarcode.substring(0, 12);
                        barcodeBehind = fbarcode.substring(13, 22);
                        size = matnrInfoList.size();
                    }

                    for (int i = 0; i < size; i++) {
                        MatnrInfo matnrInfo = matnrInfoList.get(i);
                        BarcodeInfo barcodeInfo1 = pdaService.selectBarcodeInfoByPro(matnrInfo.getMatnr(), barcodeMain, barcodeBehind);
                        if (barcodeInfo1 != null) {
                            TmMatnr tmMatnr = new TmMatnr();
                            tmMatnr.setMatnr(matnrInfo.getMatnr());
                            tmMatnr.setMaktx(matnrInfo.getMaktx());
                            tmMatnr.setMatnrMain(barcodes);
                            tmMatnr.setYkutxt(matnrInfo.getYkutxt());
                            tmMatnr.setEwbez(matnrInfo.getEwbez());
                            tmMatnr.setFunit(matnrInfo.getFunit());
                            tmMatnr.setFpackunit(matnrInfo.getFpackunit());
                            tmMatnr.setFflag(matnrInfo.getFflag());
                            tmMatnr.setFnum(matnrInfo.getFnum());
                            tmMatnr.setBarcode(barcodeMain);
                            tmMatnr.setFbarcode(barcodeInfo1.getFbarcode());
                            tmMatnr.setFid(barcodeInfo1.getFid());
                            tmMatnr.setStatus(0);
                            tmMatnr.setInStockId(inStockinDetail.getStockinId());
                            tmMatnr.setInStockDId(inStockinDetail.getStockinDetailId());
                            matnrService.insertTmMatnr(tmMatnr);
                        }
                    }
                }
            } else {
                map.put("isnull", "此物料没有单据，或扫描条码已出库");
            }
        }
        return map;
    }

    /**
     * 配套入库
     *
     * @param fbarcode
     * @return
     */
    @RequestMapping("/tms/listCg")
    @ResponseBody
    public Map selectTmsMsgCg(String fbarcode, String trayCode) {
        Map map = new HashMap<>();
        Barcode barcode = null;
        Barcode barcode1 = pdaService.selectBarcodeBd(fbarcode);
        if (barcode1 == null) {
            barcode = barcodeService.selectBarcodeByIdHs(fbarcode);
            if (barcode != null) {
                pdaService.saveBarcode(barcode);
            }
        } else {
            barcode = barcode1;
        }
        String stockinGuid = "";
        if (barcode != null) {
            TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
            if (tmMatnr1 != null && tmMatnr1.getStatus() > 1) {
                map.put("status", tmMatnr1.getStatus());
                return map;
            } else if (tmMatnr1 == null) {
                map.put("status", "此条码未收货");
            }
            if (tmMatnr1 != null) {
                ZflWmsDnPostDn zflWmsDnPostDn = zflWmsDnPostDnService.selectZflWmsDnPostDnByPAI(tmMatnr1.getMatnrMain(), tmMatnr1.getInStockId());
                if (zflWmsDnPostDn != null && "X".equals(zflWmsDnPostDn.getTYPE()) && zflWmsDnPostDn.getPRUEFLOS() != null) {
                    map.put("status", "此条码检验不通过，不能入库");
                }
                if (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getTYPE()) && zflWmsDnPostDn.getPRUEFLOS() != null) {
                    map.put("status", "此条码未检验，不能入库");
                }
                if (zflWmsDnPostDn == null || (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getStatus()))) {
                    map.put("status", "此条码未检验，不能入库");
                }
            }
            //用物料和单据号查询子单据信息，如果有此物料的单据
//            String barcodes = String.format("%018d", Long.parseLong(barcode.getMatnr().split("-")[0]));
            List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfobd(barcode.getMatnr().split("-")[0]);


            if (matnrInfoList.size() == 0) {
                map.put("status", "此条码母子件信息未维护");
            } else {
                map.put("nowMatnr", barcode.getMatnr());

                map.put("quantity", barcode.getFnum());
            }
            //查询tm_matnr表里是否已有此条
            //从t_barcode_info表中查询fid值


        } else {
            map.put("status", "此条码未收货1");
        }
        List<MydoJobDetail> lists = pdaService.selectJobDet(trayCode);
        if (lists.size() > 0) {
            map.put("matnrList", lists);//成品只能查出一条母件信息
            map.put("qtyout", lists.size());
        } else {
            map.put("qtyout", 0);
        }
        return map;

    }


    @RequestMapping("/saveJob")
    @ResponseBody
    public AjaxResult saveJob(Inner inner) {
        AjaxResult ajaxResult = pdaService.insetMydoJob(inner.getTrayCode(), inner.getQuantity().intValue(), inner.getFbarcode());
        return ajaxResult;

    }

    @RequestMapping("/sureIn")
    @ResponseBody
    public AjaxResult sureIn(String trayCode) {
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("1");
        }
        pdaService.updateJobStatus(trayCode);
        return AjaxResult.success("2");
    }


    @RequestMapping("/saveJobP")
    @ResponseBody
    public AjaxResult saveJobP(Inner inner) {
        AjaxResult ajaxResult = pdaService.insertMydoJobFinish(inner.getCargoSpaceId(), inner.getFbarcode(), inner.getQuantity().intValue(), inner.getTrayCode());
        return ajaxResult;
    }

    @RequestMapping("/sureInP")
    @ResponseBody
    public AjaxResult sureInP(String trayCode) {
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("1");
        }
        pdaService.completeJob(trayCode);
        return AjaxResult.success("2");
    }

    //    @Scheduled(fixedRate = 30 * 1000)
    @RequestMapping("/finish_pk")
    @ResponseBody
    public void finishPK() {

        pdaService.finishPK();
    }

    //    @Scheduled(cron = "0 0 13 * * ?")
//    @RequestMapping("/wms_data_in_del")
//    @ResponseBody
//    public void wms_data_in_del() {
//
//        pdaService.wms_data_in_del();
//    }
    static class Inner {
        private String fbarcode;
        private BigDecimal quantity;
        private String trayCode;
        private String cargoSpaceId;

        public String getCargoSpaceId() {
            return cargoSpaceId;
        }

        public void setCargoSpaceId(String cargoSpaceId) {
            this.cargoSpaceId = cargoSpaceId;
        }

        public String getFbarcode() {
            return fbarcode;
        }

        public void setFbarcode(String fbarcode) {
            this.fbarcode = fbarcode;
        }

        public BigDecimal getQuantity() {
            return quantity;
        }

        public void setQuantity(BigDecimal quantity) {
            this.quantity = quantity;
        }

        public String getTrayCode() {
            return trayCode;
        }

        public void setTrayCode(String trayCode) {
            this.trayCode = trayCode;
        }

        @Override
        public String toString() {
            return "Inner{" +
                    "fbarcode='" + fbarcode + '\'' +
                    ", quantity='" + quantity + '\'' +
                    ", trayCode='" + trayCode + '\'' +
                    '}';
        }
    }
}
